﻿using MiniFox.Web;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Text;

namespace MiniFox.Net.Models
{
    /// <summary>
    /// 
    /// </summary>
    public class WebMultiPartRequest : WebMessageRequestBase
    {
        string _boundary;
        List<(string formName, string fileName, string mimeType, byte[] data)> _fileDatas;
        /// <summary>
        /// 
        /// </summary>
        public WebMultiPartRequest() : this(new List<(string formName, string fileName, string mimeType, byte[] data)>())
        {

        }
        /// <summary>
        /// 
        /// </summary>
        public WebMultiPartRequest(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas) : this(fileDatas, null)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileDatas"></param>
        /// <param name="query"></param>
        public WebMultiPartRequest(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query) : this(fileDatas, query, null)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileDatas"></param>
        /// <param name="query"></param>
        /// <param name="headers"></param>
        public WebMultiPartRequest(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query, object headers) : base(HttpMethod.Post, query, headers)
        {
            this._boundary = $"----{SequenceGUID.NewGUID()}"; ;
            _fileDatas = new List<(string formName, string fileName, string mimeType, byte[] data)>(fileDatas);
        }
        /// <summary>
        /// 
        /// </summary>
        public string Boundary
        {
            get { return this._boundary; }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<(string formName, string fileName, string mimeType, byte[] data)> FileDatas
        {
            get
            {
                return _fileDatas;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected internal override HttpContent CreateHttpContent()
        {
            var content = new MultipartContent("form-data", this.Boundary);
            foreach(var fileData in this._fileDatas)
            {
                var subContent = new ByteArrayContent(fileData.data);
                content.Add(subContent);
            }
            return content;
        }
    }
}
